<?php
require_once 'BaseController.php';

/**
 * 【PC版】店舗問い合わせ コントローラ
 */
class Renew_ShopentryController extends Renew_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $this->doAction();
    }

    /**
     * メインアクション(post)
     */
    public function postAction()
    {
        $this->doAction();
    }

    private function doAction()
    {
        $params = $this->getParameters();

        $code = $params['ShopCd'];
        $step = $params['Step'];

        $this->_data['params'] = $params;

        $this->makeTemplateParams($code);

        $p_campaigns = $this->collectCampaignFromParams($params);
        $this->_data['p_campaigns'] = $p_campaigns;

        $model_cam = new Renew_Model_BukkenCampaign();
        $temp = $model_cam->getNormalCampaigns(); // キャンペーンデータ取得

        if (empty($step))
        {
            $this->_data['CAMPAIGNS'] = $temp;
            $this->displayBody2('entry');
        }
        else if ($step == 1)
        {

            if ($this->checkParams($params))
            {
                // 入力にエラーがある場合
                $campaigns = array();
                foreach ($temp as $k => $cam)
                {
                    if (in_array($cam['M94_CAMPAIGN_CD'], $p_campaigns))
                    {
                        $cam['name'] = 'campaign' . $k;
                        $cam['checked'] = 1;
                    }
                    $campaigns[] = $cam;
                }
                $this->_data['CAMPAIGNS'] = $campaigns;
                // エラー画面へ遷移
                $this->displayBody2('error');
            }
            else
            {
                // 入力にエラーがない場合
                $campaigns = array();
                foreach ($temp as $k => $cam)
                {
                    if (in_array($cam['M94_CAMPAIGN_CD'], $p_campaigns))
                    {
                        $cam['name'] = 'campaign' . $k;
                        $campaigns[] = $cam;
                    }
                }
                $this->_data['CAMPAIGNS'] = $campaigns;
                // 確認画面へ遷移
                $this->displayBody2('confirm');
            }
        }
        else if ($step == 2)
        {

            $cparams = array();
            $cparams['Tel'] = $params['Tel'];
            $cparams['Mail'] = $params['Mail'];
            $cparams['vintGenruFlg'] = 0;
            $cparams['NameSei'] = $params['KanjiSei'];
            $cparams['NameMei'] = $params['KanjiMei'];
            // 20141211 S_Add
            $cparams['NameSeiKana'] = $params['KanaSei'];
            $cparams['NameMeiKana'] = $params['KanaMei'];
            $cparams['IS_FREE_CALL'] = $params['IS_FREE_CALL']; // 無料電話使用有無
                                                                  // 20141211 E_Add

            $cparams['CONVERT_CONNECTION_NO'] = $params['CONVERT_CONNECTION_NO']; // 20150128 Add

            $list = $this->extractParams($params, 'ShopCd');
            $cparams['ShopCd'] = join(',', $list);

            $list = $this->extractParams($params, 'check');
            if (array_key_exists('check1', $params)) $cparams['Inquiry1'] = 1;
            if (array_key_exists('check2', $params)) $cparams['Inquiry2'] = 1;
            if (array_key_exists('check3', $params)) $cparams['Inquiry3'] = 1;
            if (array_key_exists('check4', $params)) $cparams['Inquiry4'] = 1;

            $cparams['Comment'] = $params['shosai'];

            $list = $this->extractParams($params, 'campaign');
            $cparams['CampaignAgechau'] = join(';', $list);
            $cparams['TimeStr'] = date("Y/m/d H:i:s");

            $this->callInquiryApi('apishop', $cparams);

            $this->my_secure_redirect('inquiry/shopentry/-/complete/');
        }
        else if ($step == 'complete')
        {
            // 検討リスト表示
            $this->loadData();
            $this->displayBody2('complete');
        }
    }

    private function makeTemplateParams($code)
    {
        $model = new Shop_Model_Detail();

        $params = array();
        $params['ShopCd'] = $code;

        $data = $model->getList($params);

        $apalogurl = $this->_config['apm']['apl']['apalog']['baseurl'];
        $tenpoimageurl = $this->_config['apm']['apl']['imagesvurl'] . 'fs/tenpoimg/';
        $data = $model->convertData($data[0], $apalogurl, $tenpoimageurl);
        $this->_data['shop'] = $data;
    }

    /**
     * 入力チェック
     *
     * @param unknown $params リクエストパラメータ
     * @return number 0:OK,1:NG
     */
    private function checkParams($params)
    {
        $error = 0;

        // 姓・名の全角文字チェック
        $key = array();
        $key[] = 'kanji_sei';
        $key[] = 'kanji_mei';
        foreach ($key as $k)
        {
            if ($this->checkZenkaku($params[$k]))
            {
                $this->_data['name_error'] = 1;
                $this->_data[$k . '_error'] = "error";
                $error = 1;
            }
        }
        // セイ・メイの全角カナ文字チェック
        $key = array();
        $key[] = 'kana_sei';
        $key[] = 'kana_mei';
        foreach ($key as $k)
        {
            if ($this->checkZenkakuKana($params[$k]))
            {
                $this->_data['name_error'] = "error";
                $this->_data[$k . '_error'] = "error";
                $error = 1;
            }
        }
        // メールアドレス・電話番号チェック
        $params['mail'] = trim($params['mail']);
        $params['tel'] = trim($params['tel']);
        if (empty($params['mail']) && empty($params['tel']))
        {
            // メールアドレス・電話番号の両方が未入力の場合
            $this->_data['renraku_empty_error'] = "error";
            $this->_data['renraku_error'] = "error";
            $error = 1;
        }
        else
        {
            if (!empty($params['mail']) && $this->checkMail($params['mail']))
            {
                // メールアドレスが入力されているかつ、入力形式が正しくない場合
                $this->_data['mail_error'] = "error";
                $this->_data['renraku_error'] = "error";
                $error = 1;
            }
            if (!empty($params['tel']) && !preg_match("/^[0-9]+$/u", $params['tel']))
            {
                // 電話番号が入力されているかつ、入力形式が正しくない場合
                $this->_data['tel_error'] = "error";
                $this->_data['renraku_error'] = "error";
                $error = 1;
            }
        }
        // お問い合わせ内容チェック
        $checks = 0;
        for ($i = 1; $i <= 4; $i++)
        {
            // チェックボックス４つ分をループ
            if ($params['check' . $i] == 'on')
            {
                // チェックボックスonがあれば、+1
                $checks++;
            }
        }
        if ($checks == 0)
        {
            // チェックボックスがすべてoffの場合
            $this->_data['checks_error'] = "error";
            $error = 1;
        }
        if (strlen($params['shosai']) > 500)
        {
            // 自由記載欄の文字数が500文字以上の場合
            $this->_data['shosai_error'] = "error";
            $error = 1;
        }

        return $error;
    }

    /**
     * SEO対策用のmetaタグに配置する文章を設定
     */
    protected function setSeoMeta()
    {
        $params = $this->getParameters();

        $subTitle = '';

        if ($params['Step'] == 1)
        {
            $subTitle = ' 入力内容の確認 ';
        }
        else if ($params['Step'] == 'complete')
        {
            $subTitle = ' 送信完了';
        }

        $this->_meta_title = '店舗へのお問い合わせ' . $subTitle . '【アパマンショップ】';
    }

}
?>
